#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <asm/mach/map.h>
#include <asm/string.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>

#define KEYINPUT_CNT  1   
#define KEYINPUT_NAME "keyinput"    
#define KEY0VALUE     0X01    
#define INVAKEY       0XFF
#define KEY_NUM       1

struct irq_keydesc {
    int gpio;                /* gpio */
    int irqnum;              /* 中断号 */
    unsigned char value;    /* 按键对应的值 */
    char name[10];         /* 名字*/
    irqreturn_t (*handler)(int, void *);   /* 中断服务函数*/
};


struct keyinput_dev
{
    dev_t devid;                /* 中断号 */
    struct cdev cdev;           /* cdev */
    struct class* class;        /* 类 */
    struct device* device;
    struct device_node* nd;
    struct timer_list timer;         
    struct irq_keydesc irqkeydesc; /* 案件描述组 */
    unsigned char curkeynum;            /* 当前的按键号 */
    struct input_dev* inputdev;    /* input结构体 */
};

struct keyinput_dev keyinputdev;


void timer_function(unsigned long arg)
{
    unsigned char value;
    unsigned char num;
    struct irq_keydesc* keydesc;
    struct keyinput_dev* dev = (struct keyinput_dev*)arg;

    num = dev->curkeynum;
    keydesc = &dev->irqkeydesc;
    value = gpio_get_value(keydesc->gpio);

    if (value == 0)
    {
        input_report_key(dev->inputdev, keydesc->value, 1);
        input_sync(dev->inputdev);
    }
    else {       /* 按键松开 */
        input_report_key(dev->inputdev, keydesc->value, 0);
        input_sync(dev->inputdev);
    }
    
}

irqreturn_t key0_handler(int irq, void* dev_id)
{
    struct keyinput_dev *dev = (struct keyinput_dev *)dev_id;

    dev->curkeynum = 0;
    dev->timer.data = (volatile long)dev_id;
    mod_timer(&dev->timer, jiffies + msecs_to_jiffies(10));
    return IRQ_RETVAL(IRQ_HANDLED);
}

static void keyio_init(void)
{
    int  ret = 0;
    keyinputdev.nd = of_find_node_by_path("/key");
    if (keyinputdev.nd == NULL)
    {
        printk("key node not find!\r\n");
        return -EINVAL;
    }

    keyinputdev.irqkeydesc.gpio = of_get_named_gpio(keyinputdev.nd, "key-gpio", 0);
    if (keyinputdev.irqkeydesc.gpio < 0)
    {
        printk("can't get key0");
    }

    /* 初始化key所使用的IO，并且设置成中断模式 */
    memset(keyinputdev.irqkeydesc.name, 0, sizeof(keyinputdev.irqkeydesc.name));
    sprintf(keyinputdev.irqkeydesc.name, "key0");
    gpio_request(keyinputdev.irqkeydesc.gpio, keyinputdev.irqkeydesc.name);
    gpio_direction_input(keyinputdev.irqkeydesc.gpio);
    keyinputdev.irqkeydesc.irqnum = irq_of_parse_and_map(keyinputdev.nd, 0);

    /* 申请中断 */
    keyinputdev.irqkeydesc.handler = key0_handler;
    keyinputdev.irqkeydesc.value = KEY_0;

    ret = request_irq(keyinputdev.irqkeydesc.irqnum,
                     keyinputdev.irqkeydesc.handler,
                     IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
                     keyinputdev.irqkeydesc.name, &keyinputdev);
    if (ret <0)
    {
        printk("irq %d request failed !\r\n", keyinputdev.irqkeydesc.irqnum);
        return -EFAULT;
    }

    /* 创建定时器 */
    init_timer(&keyinputdev.timer);
    keyinputdev.timer.function = timer_function;
    /* 申请input_dev */
    keyinputdev.inputdev = input_allocate_device();
    keyinputdev.inputdev->name = KEYINPUT_NAME;
    keyinputdev.inputdev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
    input_set_capability(keyinputdev.inputdev, EV_KEY, KEY_0);

    /* 注册输入设备 */
    ret = input_register_device(keyinputdev.inputdev);
    if (ret)
    {
        printk("register input device failed!\r\n");
        return ret;
    }
    return 0;
}

static __init keyinput_init(void)
{
    keyio_init();
    return 0;
}

static __exit keyinput_exit(void)
{

}



module_init(keyinput_init);
module_exit(keyinput_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangchaoliang");
